<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="imgeUrl">
      <single-upload v-model="dataForm.imgeUrl"></single-upload>    
    </el-form-item>
    <el-form-item label="商品类型" prop="goodsTpye">
        <el-input v-model="dataForm.goodsTpye" placeholder="商品类型"></el-input>
    </el-form-item>
    <el-form-item label="商品进价" prop="goodsInprice">
      <el-input v-model="dataForm.goodsInprice" placeholder="商品进价"></el-input>
    </el-form-item>
    <el-form-item label="商品售价" prop="goodsOtprice">
      <el-input v-model="dataForm.goodsOtprice" placeholder="商品售价"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="逻辑删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from 'axios'
import SingleUpload from "./singleUpload.vue"
  export default {
    components:{SingleUpload},
    data () {
      return {
        visible: false,
        goodsTypeList:[],
        dataForm: {
          goodsId: 0,
          goodsName: '',
          imgeUrl: '',
          goodsTpye: '',
          goodsInprice: '',
          goodsOtprice: '',
          isDelete: ''
        },
        dataRule: {
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          imgeUrl: [
            { required: false, message: '商品图片不能为空', trigger: 'blur' }
          ],
          goodsTpye: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ],
          goodsInprice: [
            { required: true, message: '商品进价不能为空', trigger: 'blur' }
          ],
          goodsOtprice: [
            { required: true, message: '商品售价不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created(){
      this.getGoodsTypeList()
    },
    methods: {
      //是否显示选中的值
      display(value){
      	console.log(value)
      },
      getGoodsTypeList(){
        axios.get(this.$http.adornUrl(`/admin/ssmgoodstype/list?limit=100`)).then(res => {
             this.goodsTypeList = res.data.page.list
             console.log(this.goodsTypeList)

            })
      },
      init (id) {
        this.dataForm.goodsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.goodsId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/ssmgoods/info/${this.dataForm.goodsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsName = data.ssmGoods.goodsName
                this.dataForm.imgeUrl = data.ssmGoods.imgeUrl
                this.dataForm.goodsTpye = data.ssmGoods.goodsTpye
                this.dataForm.goodsInprice = data.ssmGoods.goodsInprice
                this.dataForm.goodsOtprice = data.ssmGoods.goodsOtprice
                this.dataForm.isDelete = data.ssmGoods.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/ssmgoods/${!this.dataForm.goodsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'goodsId': this.dataForm.goodsId || undefined,
                'goodsName': this.dataForm.goodsName,
                'imgeUrl': this.dataForm.imgeUrl,
                'goodsTpye': this.dataForm.goodsTpye,
                'goodsInprice': this.dataForm.goodsInprice,
                'goodsOtprice': this.dataForm.goodsOtprice,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
